package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.service.ReportService;
import com.sky.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.commons.lang.StringUtils;


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 营业额统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> timeList = getLocalDates(begin, end);

        //定义营业额集合
        List<Double> turnoverList=new ArrayList<>();
        for (LocalDate date : timeList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            //查询状态为已完成的订单,将金额合并
            Map map=new HashMap<>();
            map.put("begin",beginTime);
            map.put("end",endTime);
            map.put("status", Orders.COMPLETED);
            Double sum = orderMapper.sumByMap(map);
            turnoverList.add(sum);
        }

        //数据封装
        return TurnoverReportVO.builder()
                .dateList(StringUtils.join(timeList,","))
                .turnoverList(StringUtils.join(turnoverList,","))
                .build();
    }

    /**
     * 用户统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> timeList = getLocalDates(begin, end);

        List<Integer> newUserList = new ArrayList<>(); //新增用户数
        List<Integer> totalUserList = new ArrayList<>(); //总用户数

        for (LocalDate date : timeList) {
            LocalDateTime endTime = LocalDateTime.of(date,LocalTime.MAX);
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);

            Map map=new HashMap<>();
            map.put("end",endTime);
            Integer userSum = orderMapper.countByMap(map);
            totalUserList.add(userSum);

            map.put("begin",beginTime);
            Integer newUserSum = orderMapper.countByMap(map);
            newUserList.add(newUserSum);
        }

        return UserReportVO.builder()
                .dateList(StringUtils.join(timeList,","))
                .newUserList(StringUtils.join(newUserList,","))
                .totalUserList(StringUtils.join(totalUserList,","))
                .build();
    }


    /**
     * 订单统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public OrderReportVO ordersStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> timeList = getLocalDates(begin, end);

        List<Integer> orderSumList=new ArrayList<>();//每天订单数
        List<Integer> orderValidSumList=new ArrayList<>();//每天有效订单数

        for (LocalDate date : timeList) {
            LocalDateTime endTime = LocalDateTime.of(date,LocalTime.MAX);
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);

            //订单数集合
            Map map=new HashMap<>();
            map.put("end",endTime);
            map.put("begin",beginTime);
            Integer total = orderMapper.countIdByMap(map);
            orderSumList.add(total);

            //有效订单数集合
            map.put("status",Orders.COMPLETED);
            Integer validTotal = orderMapper.countIdByMap(map);
            orderValidSumList.add(validTotal);
        }

        //一定时间内的订单总数
        Integer totalOrderCount = orderSumList.stream().reduce(Integer::sum).get();
        //一定时间内的有效订单总数
        Integer validOrderCount = orderValidSumList.stream().reduce(Integer::sum).get();
        //订单完成率
        Double orderCompletionRate=0.0;
        if(totalOrderCount!=0)
            orderCompletionRate=validOrderCount.doubleValue()/totalOrderCount;

        return OrderReportVO.builder()
                .totalOrderCount(totalOrderCount)
                .validOrderCount(validOrderCount)
                .orderCountList(StringUtils.join(orderSumList,","))
                .validOrderCountList(StringUtils.join(orderValidSumList,","))
                .orderCompletionRate(orderCompletionRate)
                .dateList(StringUtils.join(timeList,","))
                .build();
    }


    /**
     * 销量排名Top10
     * @return
     */
    @Override
    public SalesTop10ReportVO top10(LocalDate begin, LocalDate end) {
        LocalDateTime endTime = LocalDateTime.of(end,LocalTime.MAX);
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);

        Map map=new HashMap<>();
        map.put("end",endTime);
        map.put("begin",beginTime);
        map.put("status",Orders.COMPLETED);
        List<GoodsSalesDTO> salesTop10 = orderMapper.getSalesTop10(map);

        String nameList = StringUtils.join(salesTop10.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList()), ",");
        String numberList = StringUtils.join(salesTop10.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList()), ",");

        return SalesTop10ReportVO.builder()
                .nameList(nameList)
                .numberList(numberList)
                .build();
    }



    private static List<LocalDate> getLocalDates(LocalDate begin, LocalDate end) {
        //定义时间集合
        List<LocalDate> timeList=new ArrayList<>();
        timeList.add(begin);
        while (!begin.equals(end)){
            begin = begin.plusDays(1);
            timeList.add(begin);
        }
        return timeList;
    }
}
